/**
* AutoSWF - Cody Cushing. March 26, 2009
* Copyright (c) 2009 Cody Cushing
* No use restrictions. Maintain this notice in all versions. 
**/

package com.autoswf.lattice
{
	import flash.display.Loader;
	import flash.errors.IllegalOperationError;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;


	/**
	 * Lattice node for loading an image or swf. 
	 */
	public class LoaderLattice extends AsyncLattice
	{
		protected var _loader:Loader;
		public function get loader():Loader
		{
			return _loader;
		}
		
		
		/**
		 * Constructor. 
		 */
		public function LoaderLattice(url:String, context:LoaderContext = null)
		{
			_loader = new Loader();
			
			watch(_loader.contentLoaderInfo);
			completeTriggers(Event.COMPLETE).cancelTriggers(IOErrorEvent.IO_ERROR);
			
			var context:LoaderContext = context || new LoaderContext(false, ApplicationDomain.currentDomain);
			super.onExecute(_loader.load, new URLRequest(url), context);
		}
		
		
		/** Override to throw an error if the user mistakenly tries to use this method. */
		override public function onExecute(object:Object, ...rest):Lattice
		{
			throw new IllegalOperationError("This method starts the Loader, use onComplete for subsequent tasks.");
		}
	}
}